<?php

// ========== Init section ==========
// Allow inclusion for the other files
define('INCLUDE', true);

// Configuration file see doc inside
require_once 'config.php';

// Database file see doc inside (based on SQLite v2)
require_once CORE_DIRECTORY.OS_PATH_SEPARATOR.'database.php';

debug('add.php', '<a href="%v">Upload testing</a>', JGLIB_DEBUG_INFO);

// Database object is instanced here as we'll always need it,
// it will be used as a global variable to pass-through multiple instantiations
$DB = new Database(DATABASE);

// Auto setup, we make a query on the id table, if it fails we run setup
// Sorry for the @ but it's a needed trick
if(!@$DB->queryExec('SELECT * FROM id'))
{
	$DB->setup();
}

// Template engine initialisation
require_once LIBS_DIRECTORY.OS_PATH_SEPARATOR.'Smarty'.OS_PATH_SEPARATOR.'Smarty.class.php';
$smarty = new Smarty();

// Parameter processing
$ajax = false;
$type = $meta = null;
$page = 1;

if(!empty($_REQUEST['type']))
{
	$type = (int)$_REQUEST['type'];
	if(!empty($_REQUEST['page']))
	{
		$page = (int)$_REQUEST['page'];
	}
}

if(!empty($_REQUEST['meta']))
{
	$meta = (int)$_REQUEST['meta'];
}

if(!empty($_REQUEST['ajax']) && in_array($_REQUEST['ajax'], array('items', 'tags')))
{
	$ajax = $_REQUEST['ajax'];
}

// ========== Paging section ==========
$numberOfPage = debug($DB->getNumberOfPage($type, $meta), 'Number of page', JGLIB_DEBUG_INFO);
$prevPage = $nextPage = null;
if(!is_null($page))
{
	if($page > 1 && $numberOfPage > 1)
	{
		$prevPage = $page-1;
	}
	if($page < $numberOfPage)
	{
		$nextPage = $page+1;
	}
}
$smarty->assign('prevPage', $prevPage);
$smarty->assign('nextPage', $nextPage);
$smarty->assign('selectedType', $type);
$smarty->assign('selectedMeta', $meta);

// ========== Type section ==========
if(!$ajax)
{
	// Class processing for selected type
	if(isForeachable($typesTemplate)) foreach($typesTemplate as &$t1)
	{
		if($t1['id'] == $type)
		{
			$t1['class'] .= ' selected';
			$t1['none']   = '_';
		}
	}

	$smarty->assign('types', $typesTemplate);
}

// ========== Items section ==========
if(!$ajax || $ajax == 'items')
{
	$itemsTemplate = array();
	$itemsTemplate = debug($DB->getItems($type, $meta, $page), 'Items list', JGLIB_DEBUG_SQLR);

	// Thumbnail testing
	if(isForeachable($itemsTemplate)) foreach($itemsTemplate as &$i)
	{

		$hit = glob(THUMBNAIL_DIRECTORY.OS_PATH_SEPARATOR.$i['id'].'.*');
		if(!empty($hit))
		{
			$i['thumbnail'] = debug($hit[0], 'Thumbnail cache hit', JGLIB_DEBUG_CACH);
		}
		else
		{
			$i['thumbnail'] = THEME_DIRECTORY.OS_PATH_SEPARATOR.$i['type'].'.png';
		}
	}

	$smarty->assign('items', $itemsTemplate);
}

// ========== Metadata section ==========
if(!$ajax || $ajax == 'tags')
{
	$tagsTemplate = array();
	$tagsTemplate = debug($DB->getTags($type), 'Tags list', JGLIB_DEBUG_SQLR);

	$max = 0;
	// Class processing calculated from the counter
	if(isForeachable($tagsTemplate)) foreach($tagsTemplate as $t)
	{
		if($t['counter'] > $max) $max = $t['counter'];
	}

	if(isForeachable($tagsTemplate)) foreach($tagsTemplate as &$t2)
	{
		$note = $t2['counter']/$max;
	
		if($note < 0.2)     $t2['class'] = 'note1';
		elseif($note < 0.4) $t2['class'] = 'note2';
		elseif($note < 0.6) $t2['class'] = 'note3';
		elseif($note < 0.8) $t2['class'] = 'note4';
		else                $t2['class'] = 'note5';
	
		if($t2['id'] == $meta)
		{
			$t2['class'] .= ' selected';
			$t2['none']   = '_';
		}
	}

	$smarty->assign('tags', debug($tagsTemplate, 'Meta list after transformation', JGLIB_DEBUG_PER1));
}

// ========== Rendering section ==========
$timer2->addTimer();
$smarty->assign('timerPage', $timer2->getLastTime());

if(!$ajax)
{
	$smarty->display('index.tpl');
}
else
{
	$smarty->display($ajax.'.tpl');
}
require_once 'end.php';

